package com.cpe.web.intercepter.base;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.sql.data.attribute.CodeType;
import com.sql.data.response.JsonResponse;

public abstract class AbstractInterceptor extends HandlerInterceptorAdapter {

	protected static final boolean GO_ON = true;

	protected List<String> excludedMappings;

	private ObjectMapper objectMapper;

	public AbstractInterceptor() {
		objectMapper = new ObjectMapper();
		excludedMappings = Lists.newArrayList();
	}

	/**
	 * 响应被拦截下来的请求
	 */
	protected boolean end(HttpServletResponse response, CodeType codeType) throws IOException {
		response.setContentType(MediaType.APPLICATION_JSON_VALUE);
		response.setHeader(HttpHeaders.CACHE_CONTROL, "no-store");
		String jsonResult = objectMapper.writeValueAsString(new JsonResponse(codeType));
		PrintWriter pw = response.getWriter();
		pw.print(jsonResult);
		pw.flush();
		return false;
	}

	public AbstractInterceptor addExclusion(String mapping) {
		excludedMappings.add(mapping);
		return this;
	}
}
